package br.consultorio.digital.util;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import br.consultorio.digital.modelo.entidade.ParametroWebService;

public class WebServiceCliente {

	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String URL = "http://servicos.cardiol.br/ws_pop/ServicoPOP.asmx";
	private Object o;
	private HttpTransportSE http;
	private SoapSerializationEnvelope envelope;
	private static WebServiceCliente wsc;

	private WebServiceCliente() {
		if (http == null) {
			http = new HttpTransportSE(URL);
		}
		if (envelope == null) {
			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;
		}
	}

	public static  WebServiceCliente getinstance() {
		if (wsc == null) {
			wsc = new WebServiceCliente();
		}
		return wsc;
	}

	public void executeServiceCliente(String METHOD_NAME,
			ArrayList<ParametroWebService> apw) {

		try {
			SoapObject requisicao = new SoapObject(NAMESPACE, METHOD_NAME);

			for (ParametroWebService pw : apw) {
				requisicao.addProperty(pw.getNm_parametro(),
						pw.getOb_parametro());
			}
			
			envelope.setOutputSoapObject(requisicao);

			http.call(NAMESPACE + METHOD_NAME, envelope);

			o = envelope.getResponse();
		} catch (IOException e) {
			Log.e("WebServiceCliente", "Erro: " + e);
		} catch (XmlPullParserException e) {
			Log.e("WebServiceCliente", "Erro: " + e);
		}
	}

	public void executeServiceCliente(String METHOD_NAME) {

		try {
			SoapObject requisicao = new SoapObject(NAMESPACE, METHOD_NAME);

			envelope.setOutputSoapObject(requisicao);

			http.call(NAMESPACE + METHOD_NAME, envelope);

			o = envelope.getResponse();
		} catch (IOException e) {
			Log.e("WebServiceCliente", "Erro: " + e);
		} catch (XmlPullParserException e) {
			Log.e("WebServiceCliente", "Erro: " + e);
		}
	}

	public SoapObject retornaSoapObject() {
		return ((SoapObject) o);
	}

	public SoapPrimitive retornaSoapPrimitive() {
		return ((SoapPrimitive) o);
	}
	public Mensagem retornoMensagem() {

		Mensagem m = new Mensagem();

		m.setIn_retorno(Integer.parseInt(retornaSoapObject().getProperty(
				"IN_Retorno").toString()));

		m.setTx_retorno(retornaSoapObject().getProperty("TX_Mensagem")
				.toString());

		return m;
	}

	public int retornaInteiroPrimitivo() {
		return Integer.parseInt(retornaSoapPrimitive().toString());
	}

	public ArrayList<String> retornaStringPrimitivo() {
		ArrayList<String> as = new ArrayList<String>();

		for (int i = 0; i < retornaSoapObject().getPropertyCount(); i++) {
			as.add(retornaSoapObject().getProperty(i).toString());
		}
		return as;
	}

}